<template>
<!-- 添加活动 -->
  <div class='addActivityDialog'>
    <el-dialog
      title="添加活动"
      :visible.sync="isShow"
      width="40%"
      center
      :before-close="handleClose">
      <el-form :model="from" :rules="rules" ref="ruleForm" label-width="140px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="from.name"></el-input>
        </el-form-item>

        <el-form-item label="活动时间" prop="activityTime">
          <el-date-picker
            class='activityTime'
            v-model="from.activityTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            unlink-panels
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="奖励金额" prop="reward">
          <el-input v-model.number="from.reward"></el-input>
        </el-form-item>

        <el-form-item label="活动类型" prop="type">
          <el-select v-model="from.type" placeholder="请选择活动类型">
          </el-select>
        </el-form-item>

        <el-form-item label="选择活动满足条件" prop="type" >
          <el-select v-model="from.conditionsTitle" multiple placeholder="请选择活动满足条件" value-key='key' @remove-tag='removeConditionItem'>
            <el-option v-for=" (item, index) in from.conditions" :key="index" :label="item.label" :value="item.value" ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-for="(item, index) in from.conditionsItems" :key="index" :label="item.text" :prop="item.param" class="form-item">
          <el-date-picker
            class="form-item-input"
            v-if='item.text == "定稿时间"'
            v-model="from[item.param]"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            unlink-panels
            placeholder="选择日期">
          </el-date-picker>

          <el-input v-else v-model="from[item.param]" class="form-item-input"></el-input><el-button  class="form-item-btn" @click.prevent="removeConditionItem(item)">删除</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" v-no-more-click:3000 @click="addCondition()">添加活动满足条件</el-button>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" v-no-more-click:3000 @click="submit('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    // 奖励金额验证
    var validateMoney = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入奖励金额'));
      } else {
        if (!Number(value)) {
          callback(new Error('请输入正确的金额'));
        } else if (value <= 0) {
          callback(new Error('奖励金额必须大于0'));
        } else {
          callback()
        }
      }

    }
    return {
      isShow: false,
      from: {
        name: '',
        type: '',
        reward: 0,
        conditionsItems: [],
        conditionsTitle:[],
        finals_created_at: '',
        activityTime: [],
        conditions: [
          {
            label: '定稿时间',
            value: {
              text: '定稿时间',
              param: 'finals_created_at',
              value:'',
              key: 4
            },
            key: 4
          },
          // {
          //   label: '活动时间',
          //   value: {
          //     text: '活动时间',
          //     param: 'activityTime',
          //     value:'',
          //     key: 5
          //   },
          //   key: 5
          // },
        ]
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'change' }
        ],
        reward: [
          { validator: validateMoney, trigger: 'change' }
        ],
        finals_created_at: [
          { required: true, message: '请选择定稿日期', trigger: 'change' }
        ],
        activityTime: [
          { required: true, message: '请选择定稿日期', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    show () {
      this.isShow = true
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if(valid) {
          if (this.from.conditionsItems.length == 0) {
            this.$message.error('请添加活动满足条件')
          } else {
            this.submitPost()
          }
        }
      })
    },
    submitPost() {
      let params = {
        name: this.from.name,
        reward: this.from.reward,
        condition: []
      }

      for(let i = 0; i < this.from.conditionsItems.length; i++) {
        // 定稿时间
        // if (this.from.conditionsItems[i]['param'] == 'finals_created_at') {
        //   params.condition
        // }
        params.condition[this.from.conditionsItems[i]['param']] = this.from[this.from.conditionsItems[i]['param']]
      }

      // 活动时间
      params.condition.start_time = this.from.activityTime[0]
      params.condition.end_time = this.from.activityTime[1]

      if (this.from.type != "") {
        params.type = this.from.type
      }

      this.$axios.post('api/activity', params).then(res => {
        if (res.data.code == 200 ) {
          this.$message({
            message: '申请活动成功！',
            type: 'success'
          })
          this.$emit('getData')
          this.isShow = false
        } else if (res.data.code == 400) {
          this.$message.error(res.data.message)
        }

      })
    },
    // 添加活动满足条件
    addCondition () {
      let  verifyActivityTime = false
      for (let i = 0; i < this.from.conditionsTitle.length; i++) {
        if (this.from.conditionsTitle[i].param == "activityTime") {
          verifyActivityTime = true
        }
        this.from.conditionsItems.push(this.from.conditionsTitle[i])
        this.from[this.from.conditionsTitle[i].param] = ''
      }
      // console.log(verifyActivityTime)
      // if (!verifyActivityTime) {
      //   this.$message.error('活动时间为必选条件，请选择后重新添加。')
      //   this.from.conditionsItems = []
      // }


    },
    // 删除活动满足条件
    removeConditionItem (item) {
      // if (item.param == 'activityTime') {
        // this.$message.error('活动时间为必选条件，无法删除。')
        // this.from.conditionsTitle.push(item.text)
      // } else {

      // }
      var index = this.from.conditionsItems.indexOf(item)
        if (index !== -1) {
          this.from.conditionsItems.splice(index, 1)
        }
    },
    handleClose () {
      this.isShow = false
      this.$refs.ruleForm.resetFields();
      this.from.conditionsItems = []
      this.from.conditionsTitle = []
    }
  }
}
</script>

<style lang='less' type='text/css'>
  .addActivityDialog {
    .el-dialog {
      min-width: 600px;
    }
    .el-select {
      width: 100%;
    }
    .activityTime{
      width: 100%;
    }
    .form-item {
      .el-form-item__content::after {
        content: "";
        display: block;
        height: 0;
        clear:both;
        visibility: hidden;
      }
    }
    .form-item-input {
      float: left;
      width: 80%;
    }
    .form-item-btn {
      float: right;
    }
  }

</style>
